/*
 * QuestionnaireController.java
 *
 * Created on January 7, 2008, 11:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package sqs.web;

/**
 *
 * @author Joko YU
 */

import javax.servlet.http.HttpSession;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;
import sqs.model.Question;
import sqs.model.Questionnaire;
import sqs.delegates.QuestionnaireMgrDelegate;

import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class QuestionnaireController implements Controller {
    
    
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException {
        

        String path = request.getPathInfo();
        
        if (path.equals("/") || path.equals("")) {
            return getQnaireListView();
            
        } else if(path.equals("/process.htm")) {

            Questionnaire qnaire = null;
            QuestionnaireMgrDelegate ctrler = new QuestionnaireMgrDelegate();

            int qnaireID = Integer.parseInt(request.getParameter("QID"));
            qnaire = ctrler.getQuestionnaire(qnaireID);

            List<Question> qnList = qnaire.getQuestions();
            List<String> ansList = new ArrayList<String>();

            boolean incomplete = false;
            for(int i=0; i<qnList.size(); i++) {
                Question qn = qnList.get(i);
                String ans = (String)request.getParameter("Q"+qn.getQuestionId());
                if((qn.isCompulsory()) && (ans==null)) incomplete=true;
                ansList.add(ans);
            }
            
            if(incomplete) {
                ModelAndView mav = new ModelAndView("questionnaire", "qnaire", qnaire);
                mav.addObject("ans", ansList);
                mav.addObject("incomplete", "yes");
                return mav;
            } else 
                return new ModelAndView("thankyou");
            
        } else {
            Questionnaire qnaire = null;
            QuestionnaireMgrDelegate ctrler = new QuestionnaireMgrDelegate();            
            qnaire = ctrler.getQuestionnaire(Integer.parseInt(path.substring(1)));
            
            if(qnaire!=null)
                return new ModelAndView("questionnaire", "qnaire", qnaire);
            else
                return getQnaireListView();
        }
    }
    /*
    protected ModelAndView onSubmit(Object command) throws ServletException {
        return new ModelAndView(getSuccessView());
    }
     
    protected ModelAndView showForm(HttpServletRequest request,
                                HttpServletResponse response,
                                BindException errors)
                         throws Exception {
     
        String path = request.getPathInfo();
        if (path.equals("/") || path.equals("")) {
            return getQnaireListView();
     
        } else {
            //return new ModelAndView("forward: questionnaire.htm");
     
            Questionnaire qnaire = null;
            QuestionnaireMgrDelegate ctrler = new QuestionnaireMgrDelegate();
            qnaire = ctrler.getQuestionnaire(Integer.parseInt(path.substring(1)));
     
            if(qnaire!=null) {
                return new ModelAndView("questionnaire", "data", qnaire);
            } else {
                return getQnaireListView();
            }
        }
    }
     */

        private ModelAndView getQnaireListView() {
            List<Questionnaire> data = null;
            QuestionnaireMgrDelegate ctrler = new QuestionnaireMgrDelegate();
            data = ctrler.getAllQuestionnaires((boolean)true);
            
            // Display into the JSP
            return new ModelAndView("questionnairelist", "data", data);
        }
    
}